<?php

/**
 * personal actions.
 *
 * @package    cronos-doctrine
 * @subpackage personal
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class areaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      
      
      $area = new Area();
      
      //Hace permanente la pagina en una variable de sesion
      if($request->getParameter('page')){
          $this->getUser()->setAttribute('page_area', $request->getParameter('page'));
      }
      
      $this->areas = new sfDoctrinePager('Area', sfConfig::get('app_x_pag'));
      $this->areas->setQuery($area->getArea());
      $this->areas->setPage($this->getUser()->getAttribute('page_area'));
      $this->areas->init();
      
      $zona = new Zona();
      $this->zonas = $zona->getTotalZonas();
      
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AreaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AreaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($area = Doctrine_Core::getTable('Area')->find(array($request->getParameter('id'))), sprintf('Object Area does not exist (%s).', $request->getParameter('id')));
    $this->form = new AreaForm($area);
    
      //Zonas Asociadas internamente
      $zona = new Zona();
      ${'page_zona_int'.$request->getParameter('id')} = 'page_zona_int'.$request->getParameter('id');
      //Hace permanente la pagina en una variable de sesion
      if($request->getParameter('page')){
          $this->getUser()->setAttribute(''.${'page_zona_int'.$request->getParameter('id')}.'', $request->getParameter('page'));
      }

      $this->zonas = new sfDoctrinePager('Zona', 8);
      $this->zonas->setQuery($zona->getZonaId($request->getParameter('id')));
      $this->zonas->setPage($this->getUser()->getAttribute(${'page_zona_int'.$request->getParameter('id')}));
      $this->zonas->init();
      
      
      $parroquia = new Parroquia();
      $this->parroquias = $parroquia->getTotalParroquiasCount();
      
      
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($area = Doctrine_Core::getTable('Area')->find(array($request->getParameter('id'))), sprintf('Object Area does not exist (%s).', $request->getParameter('id')));
    $this->form = new AreaForm($area);

    $this->processForm($request, $this->form);
    
     //Zonas Asociadas internamente
      $zona = new Zona();
      ${'page_zona_int'.$request->getParameter('id')} = 'page_zona_int'.$request->getParameter('id');
      //Hace permanente la pagina en una variable de sesion
      if($request->getParameter('page')){
          $this->getUser()->setAttribute(''.${'page_zona_int'.$request->getParameter('id')}.'', $request->getParameter('page'));
      }

      $this->zonas = new sfDoctrinePager('Zona', 8);
      $this->zonas->setQuery($zona->getZonaId($request->getParameter('id')));
      $this->zonas->setPage($this->getUser()->getAttribute(${'page_zona_int'.$request->getParameter('id')}));
      $this->zonas->init();
      
     $parroquia = new Parroquia();
     $this->parroquias = $parroquia->getTotalParroquiasCount(); 

    $this->setTemplate('edit');
    
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($area = Doctrine_Core::getTable('Area')->find(array($request->getParameter('id'))), sprintf('Object Area does not exist (%s).', $request->getParameter('id')));
    $area->delete();

    $this->redirect('area/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
       
      $area = $form->save();

      $this->redirect('area/edit?id='.$area->getId());
    }
  }
}
